<?php
class DeviceMonitorController extends RController
{
	/**
	 * @var string the default layout for the views. Defaults to '//layouts/column2', meaning
	 * using two-column layout. See 'protected/views/layouts/column2.php'.
	 */
	public $defaultAction = 'admin';
	public $layout='//layouts/lay_column2';
	public $menu=array();
	public $moduleName="设备管理";	//此处修改本模块名称

	public function init()
	{
		$leftmenu = 'jichu';	//此处修改模块所属菜单
		$this->menu = Yii::app()->params[$leftmenu];
	}

	/**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'rights', // perform access control for CRUD operations
		);
	}

	/**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules()
	{
		return array(
				array('allow',  // allow all users to perform 'index' and 'view' actions
						'actions'=>array('index','view','getCitys','getCountys'),
						'users'=>array('*'),
				),
				array('allow', // allow authenticated user to perform 'create' and 'update' actions
						'actions'=>array('create','update', 'pick', 'getstatus'),
						'users'=>array('@'),
				),
				array('allow', // allow admin user to perform 'admin' and 'delete' actions
						'actions'=>array('admin','delete'),
						'users'=>array('admin'),
				),
				array('deny',  // deny all users
						'users'=>array('*'),
				),
		);
	}
	
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);
	
	echo 'enter Update data';
		// Uncomment the following line if AJAX validation is needed
		// $this->performAjaxValidation($model);
	
		if(isset($_POST['BtBatterys']))
		{
			$model->attributes=$_POST['BtBatterys'];
			if($model->save())
				$this->redirect(array('view','id'=>$model->RECORD_ID));
		}
	
		$this->render('update',array(
				'model'=>$model,
		));
	}
	
	public function actionView($id)		
	{
		echo 'enter View data';
		$this->render('view',array(
				'model'=>$this->loadModel($id),
		));
	}
	
	public function actionPick()
	{
		echo 'enter pick data';
		$client = new EHttpClient('http://127.0.0.1:8888', array(
				'maxredirects' => 3,
				'timeout'      => 30,
				'adapter'      => 'EHttpClientAdapterCurl'));
		
		$client->resetParameters();
		$client->setParameterGet(array('num'=>'2', 'p1'=>'com1', 'p2'=>'com2'));
		
		$response = $client->request();
		
		echo $response->getStatus();//200(OK), 404(not found), 400/500 timeout
		echo '<br>';
		echo $response->getHeadersAsString();
		echo '<br>';
		if($response->isSuccessful())
			echo $response->getBody();
		else
			echo $response->getRawBody();
	}
	
	public function actionGetstatus(){
		echo 'enter get status';
		
		$client = new EHttpClient('http://127.0.0.1:8888', array(
				'maxredirects' => 3,
				'timeout'      => 30,
				'adapter'      => 'EHttpClientAdapterCurl'));
		
		$client->resetParameters();
		$client->setParameterGet(array('num'=>'2', 'p1'=>'com1', 'p2'=>'com2'));
		
		$response = $client->request();
		
		echo $response->getStatus();//200(OK), 404(not found), 400/500 timeout
		echo '<br>';
		echo $response->getHeadersAsString();
		echo '<br>';
		if($response->isSuccessful())
			echo $response->getBody();
		else
			echo $response->getRawBody();
	}

	public function actionGetCitys()
	{
		$rs = '';
		$data = MtsysCity::model()->findAll('PROVINCE_ID=:PID',array(':PID'=>$_POST['PROVINCE_ID']));
		$data=CHtml::listData($data,'CITY_ID','CITY_NAME');
	
		if(!empty($data)){
			foreach($data as $value=>$name){
				$rs .= CHtml::tag('option',array('value'=>$value),CHtml::encode($name),true);
			}
		}
		$rs .= CHtml::tag('option',array('value'=>'0'),CHtml::encode('无下级单位'),true);
	
		echo $rs;
	}
	
	public function actionGetCountys()
	{
		$data=MtsysCounty::model()->findAll('CITY_ID=:CID',
				array(':CID'=>(int)$_POST['CITY_ID']));
	
		$data=CHtml::listData($data,'COUNTY_ID','COUNTY_NAME');
		if(!empty($data)){
			foreach($data as $value=>$name){
				echo CHtml::tag('option',array('value'=>$value),CHtml::encode($name),true);
			}
		}
		echo CHtml::tag('option',array('value'=>'0'),CHtml::encode('无下级单位'),true);
	}
	
	public function actionAdmin()
	{
		echo 'enter admin data';
		$model=new BtBatterys('search');
		$setting = new Setting();
		$provinces = MtsysProvince::model()->findAll();
		
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['BtBatterys']))
			$model->attributes=$_GET['BtBatterys'];
		
	
		$this->render('admin',array(
				'model'=>$model,
				'setting' => $setting,
				'provinces' => $provinces,
		));
	}
	
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=BtBatterys::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}
	
	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='bt-batterys-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}